package com.lightsaber.trade.cloth.service.planning.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.entity.AuditedObject;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.service.HppBaseServiceImpl;
import com.lightsaber.trade.cloth.dao.planning.Level2DisciplineStatusDao;
import com.lightsaber.trade.cloth.entity.planning.Level2DisciplineStatus;
import com.lightsaber.trade.cloth.service.planning.Level2DisciplineStatusService;

@Service
@Component
@Transactional
public class Level2DisciplineStatusServiceImpl extends
        HppBaseServiceImpl<Level2DisciplineStatus, String> implements Level2DisciplineStatusService {

    private static final Logger log = LoggerFactory
            .getLogger(Level2DisciplineStatusServiceImpl.class);

    @Autowired
    private Level2DisciplineStatusDao level2DisciplineStatusDao;

    @Override
    protected BaseDao<Level2DisciplineStatus, String> getDao() {
        return level2DisciplineStatusDao;
    }

    @Override
    public Level2DisciplineStatus findDisciplineStatus(final String level2PlanningId,
            final String discipline, final String categoryNodeId) {
        return level2DisciplineStatusDao.findDisciplineStatus(level2PlanningId, discipline,
                categoryNodeId);

    }

    @Override
    public List<Level2DisciplineStatus> findByLevel2IdAndNodeid(final String level2PlanningId,
            final String categoryNodeId) {
        return level2DisciplineStatusDao.findByLevel2IdAndNodeid(level2PlanningId, categoryNodeId);

    }

    @Override
    public Level2DisciplineStatus findById(final String id) {
        return level2DisciplineStatusDao.findUniqueBy("id", id);

    }

    @Override
    public void save(final Level2DisciplineStatus level2DisciplineStatus) {
        if (StringUtils.isBlank(level2DisciplineStatus.getId())) {
            level2DisciplineStatusDao.save(level2DisciplineStatus);
        } else {
            Level2DisciplineStatus old = findById(level2DisciplineStatus.getId());
            BeanUtils.copyProperties(level2DisciplineStatus, old, new String[] { BaseEntity.ID,
                    BaseEntity.VERSION, "level2Planning", "categoryNode", AuditedObject.CREATED_BY,
                    AuditedObject.CREATED_DATE });
            level2DisciplineStatusDao.update(old);
        }

    }

    @Override
    public boolean checkAllDisciplineIsNotRejectByLevel2Id(final String level2PlanningId) {
        List<Level2DisciplineStatus> list = level2DisciplineStatusDao
                .checkAllDisciplineIsNotRejectByLevel2Id(level2PlanningId);
        if (list.isEmpty()) {
            return true;
        } else {
            return false;
        }

    }

}
